/*
 * Filename DeviceService.java 2022年04月19日
 * Copyright © Ehome Co.Ltd. All Rgiths Reserved.
 * @author wuyingchao
 */
package com.ehome.poct.terminal.service;

import cn.hutool.json.JSONUtil;
import com.ehome.exception.BizCheckedException;
import com.ehome.poct.base.api.dto.device.*;
import com.ehome.poct.base.api.dto.device.bootModule.BootInfoDTO;
import com.ehome.poct.base.api.dto.device.bootModule.TestDeviceBootInfoDTO;
import com.ehome.poct.base.api.dto.device.bootModule.TestDeviceBootingDetailDTO;
import com.ehome.poct.base.api.dto.device.bootModule.TestDeviceBootingHistDTO;
import com.ehome.poct.base.api.feign.ClinicClient;
import com.ehome.poct.base.api.feign.DeviceClient;
import com.ehome.poct.base.api.feign.device.DeviceQrCodePrintClient;
import com.ehome.poct.base.api.feign.device.DeviceQrCodePrintHisClient;
import com.ehome.poct.base.api.feign.device.TestDeviceBootingDetailFeginClient;
import com.ehome.poct.base.api.feign.device.TestDeviceBootingHistFeginClient;
import com.ehome.poct.base.api.feign.sys.MqttClient;
import com.ehome.poct.base.api.qry.device.TestDeviceQry;
import com.ehome.poct.base.api.vo.device.QrcodePrintHisDetPageVO;
import com.ehome.poct.base.api.vo.device.QrcodePrintHisPageVO;
import com.ehome.poct.base.api.vo.device.TestDevicePrinterVO;
import com.ehome.poct.core.api.feign.NCrpSaaOptimizeLimitProvinceFeginClient;
import com.ehome.poct.terminal.api.constant.AppBizCode;
import com.ehome.poct.terminal.api.dto.QrCodeHisDTO;
import com.ehome.poct.terminal.api.fegin.AgentFegin;
import com.ehome.poct.terminal.api.vo.DeviceExtInfoVO;
import com.ehome.poct.terminal.mapping.DeviceAppMapping;
import com.ehome.poct.terminal.property.SysCustomizeProperties;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import com.baomidou.mybatisplus.core.toolkit.Sequence;

/**
 * @author wyc
 * @since 2022/04/19 14:32
 **/
@Service
@AllArgsConstructor
@Slf4j
public class DeviceService {
    private final DeviceClient deviceClient;
    private final ClinicClient clinicClient;
    private final MqttClient mqttClient;
    private final DeviceAppMapping deviceMapping;
    private final AgentFegin agentRpc;
    private final NCrpSaaOptimizeLimitProvinceFeginClient provinceFeginClient;
    private final SysCustomizeProperties customizeProperties;
    private final DeviceQrCodePrintClient deviceQrCodePrintClient;
    private final DeviceQrCodePrintHisClient deviceQrCodePrintHisClient;
    private final Sequence idUtil;
    private final TestDeviceBootingHistFeginClient bootingHistFeginClient;
    /**
     * 获取设备的额外信息
     *
     * @param sno
     * @return
     */
    public DeviceExtInfoVO getExtInfo(String sno) {
        final var device = deviceClient.getBySno(sno);
        final var mqtt = mqttClient.getInfo();
        if (device == null) {
            throw new BizCheckedException(AppBizCode.DEVICE_NOT_EXITS);
        }
        if (device.getTermClinicId() == null) {
//            throw new BizCheckedException(AppBizCode.DEVICE_NOT_BINDING);
            return deviceMapping.to(device, mqtt, Boolean.TRUE,  Boolean.FALSE,   customizeProperties.getHtmlDomain(),0);
        }
        final var clinic = clinicClient.getById(device.getTermClinicId());
//        Integer limitRight = 0;
//        try {
//            AgentDetailInfoVO agentJson = Optional.ofNullable(clinic).map(r -> agentRpc.getAgentInfo(clinic.getTermAgentId())).orElse(null);
//            if (hasLimitRight(agentJson.getData().getProvinceAgentId())) {
//                limitRight = 1;
//            }
//        }catch (Exception ex) {
//            log.error("获取诊所的身份信息异常 {}",ex.getMessage());
//        }
        final var bindingDevice = deviceClient.list(new TestDeviceQry().setTermClinicId(device.getTermClinicId()).setTypes(List.of(1, 2)));
        return deviceMapping.to(device, mqtt, clinic == null || clinic.getPayType() < 20,  bindingDevice.size() >= 2, customizeProperties.getHtmlDomain(),1 );
    }

    public Boolean hasLimitRight(Long provinceId){
       return true;
    }


    public void devicePrinter(TestDevicePrinterDTO testDevicePrinterDTO) {
        deviceClient.devicePrinter(testDevicePrinterDTO);
    }

    public List<TestDevicePrinterVO> getDevicePrinter(String sno) {
        return deviceClient.getDevicePrinter(sno);
    }

    public Boolean addPrintHistory(QrCodeHisDTO dto) {
        TestDeviceQrcodePrintHisDTO data= deviceMapping.to(dto);
        data.setPrintDate(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
       return deviceQrCodePrintClient.insert(data);
    }

    public QrcodePrintHisPageVO hlist(Integer pageIndex,Integer pageSize){
        return deviceQrCodePrintHisClient.getList(pageIndex,pageSize);
    }


    public QrcodePrintHisDetPageVO dlist(QrCodeHisDetailDTO req){
        return deviceQrCodePrintClient.getList(req);
    }

    public Boolean getDeviceStatus(String sno) {
        TestDeviceDTO deviceDTO = deviceClient.getBySno(sno);
        return deviceDTO.getAlgorithmStatus() == 0;
    }
    /**
     * 添加开机自检详细
     * @param bootingInfoDTO 开机自检信息
     * @return true 已录入 false 失败
     */
    public Boolean addBootInfoHis(DeviceBootingInfoDTO bootingInfoDTO)
    {
        TestDeviceBootInfoDTO bootInfo = JSONUtil.toBean(bootingInfoDTO.getBootingJson(),TestDeviceBootInfoDTO.class);
        Long id= idUtil.nextId();
        return  bootingHistFeginClient.addBootInfo(
                    deviceMapping.toBootInfo(
                        deviceMapping.toDetail(bootInfo,bootingInfoDTO.getBootingJson(),bootingInfoDTO.getSno(),id),
                        deviceMapping.toHist(bootInfo,bootingInfoDTO.getSno(),id,bootingInfoDTO.getCheckTime())
                        ));
    }

}